package com.jie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jie.dao.BlogATagMapper;
import com.jie.dao.BlogMapper;
import com.jie.pojo.Blog;
import com.jie.pojo.Blog_Tag;
import com.jie.pojo.Tag;
import com.jie.service.BlogService;
import com.jie.service.TagsService;
import com.jie.service.TypeService;
import com.jie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/17/19:52
 * @Description:
 */
@Controller
@Slf4j
@RequestMapping("/")
public class TagShowController {
    @Autowired
    private TagsService tagsService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogATagMapper blogATagMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private BlogMapper blogMapper;
    @GetMapping("/tags/{id}")
    public String tags(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                        Model model, @PathVariable Long id){
        List<Tag> list = tagsService.list();
        if (id==-1){
            id=list.get(0).getId();
        }

        for (Tag tag : list) {
            LambdaQueryWrapper<Blog_Tag> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Blog_Tag::getTId,tag.getId());
            tag.setBlogList(blogATagMapper.selectList(queryWrapper));
        }
        //临时存放
        List<Tag> stringList=new ArrayList<>();
        PageHelper.startPage(pageNum,8);
        List<Blog> blogs = blogMapper.selectBlogAByTagId(id);
        for (Blog record : blogs) {
            record.setUser(userService.list().get(0));
            record.setTypes(typeService.getById(record.getType()));
            //标签
            LambdaQueryWrapper<Blog_Tag> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Blog_Tag::getBId,record.getId());
            List<Blog_Tag> blogTagList = blogATagMapper.selectList(queryWrapper);
            for (Blog_Tag blog_tag : blogTagList) {
                LambdaQueryWrapper<Tag> tagLambdaQueryWrapper=new LambdaQueryWrapper<>();
                tagLambdaQueryWrapper.eq(Tag::getId,blog_tag.getTId());
                Tag one = tagsService.getOne(tagLambdaQueryWrapper);
                stringList.add(one);
            }
            record.setTagList(stringList);
            //清洗
            stringList=new ArrayList<>();
        }

        PageInfo<Blog> userList=new PageInfo<Blog>(blogs);


//
//        List<Blog> blogs = blogService.selectBlogByTagId(id);
//        PageInfo<Blog> userList=new PageInfo<>(blogs);
//        for (Blog record : userList.getList()) {
//            record.setUser(userService.list().get(0));
//            record.setTypes(typeService.getById(record.getType()));
//        }

//        LambdaQueryWrapper<Blog_Tag> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(Blog_Tag::getTId,id);
//        List<Blog_Tag> blogTagList = blogATagMapper.selectList(queryWrapper);
//
//        //临时存放
//        List<Blog> list1=new ArrayList<>();
//
//        for (Blog_Tag blog_tag : blogTagList) {
//            LambdaQueryWrapper<Blog> queryWrapper1=new LambdaQueryWrapper<>();
//            queryWrapper1.eq(Blog::getId,blog_tag.getBId());
//            queryWrapper1.orderByDesc(Blog::getUpdateTime);
//            list1.add(blogService.getOne(queryWrapper1));
//        }
//
//        Page<Blog> page1=new Page<>(page,pageSize);
//        page1.setRecords(list1);
//        list1=new ArrayList<>();
//        //博客里面的多标签
//        for (Blog record : page1.getRecords()) {
//            LambdaQueryWrapper<Blog_Tag> queryWrapper2=new LambdaQueryWrapper<>();
//            queryWrapper2.eq(Blog_Tag::getBId,record.getId());
//            List<Blog_Tag> blogTagLists = blogATagMapper.selectList(queryWrapper2);
//            record.setUser(userService.list().get(0));
//            record.setTypes(typeService.getById(record.getType()));
//            for (Blog_Tag blog_tags : blogTagLists) {
//                LambdaQueryWrapper<Tag> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//                lambdaQueryWrapper.eq(Tag::getId,blog_tags.getTId());
//                List<Tag> list2 = tagsService.list(lambdaQueryWrapper);
//                record.setTagList(list2);
//            }
//        }

//        Page<Blog> pages=null;
//        for (Tag tag : list) {
//            for (Blog_Tag blog_tag : tag.getBlogList()) {
//                LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
//                queryWrapper.eq(Blog::getId,blog_tag.getBId());
//                queryWrapper.orderByDesc(Blog::getUpdateTime);
//                pages = blogService.page(pageInfo, queryWrapper);
//            }
//        }

//        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(Blog::getType,id);
//        queryWrapper.orderByDesc(Blog::getUpdateTime);
//        //b-list
//        for (Type type : list) {
//            LambdaQueryWrapper<Blog> queryWrappers=new LambdaQueryWrapper<>();
//            queryWrappers.eq(Blog::getType,type.getId());
//            List<Blog> list1 = blogService.list(queryWrappers);
//            type.setBlogList(list1);
//        }
//
//        Page<Blog> page1 = blogService.page(pageInfo, queryWrapper);
//        for (Blog record : page1.getRecords()) {
//            record.setUser(userService.list().get(0));
//            record.setTypes(typeService.getById(record.getType()));
//        }

        model.addAttribute("tags",list);
        model.addAttribute("page",userList);
        model.addAttribute("activeId",id);
        return "tags";
    }

}
